package org.hardstone.controller;

import org.hardstone.domain.BaseCategoryDomain;
import org.hardstone.service.BaseCategoryService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.Serializable;
import java.util.NoSuchElementException;

public abstract class BaseCategoryController<T extends BaseCategoryDomain<PK>, PK extends Serializable, M extends BaseCategoryService<T, PK>> {
    protected M service;
    protected PK id;

    public BaseCategoryController(M service) {
        this.service = service;
    }

    @RequestMapping("/findById/{id}")
    ResponseEntity<T> findById(@PathVariable("id") PK id) {
        try {
            T result = service.findById(id).get();
            if (result.equals(new Object())) {
                return ResponseEntity.notFound().build();
            } else {
                return ResponseEntity.ok(result);
            }
        } catch (NoSuchElementException e) {
            return ResponseEntity.notFound().build();
        }
    }
}
